package nancal.mp

import nancal.mp.db.mongo.mor
import nbcp.base.comm.JsonMap
import nbcp.base.comm.const
import nbcp.base.extend.ConvertJson
import nbcp.base.extend.ToJson
import nbcp.myoql.db.mongo.query
import org.junit.jupiter.api.Test
import java.io.File

class InitFlywayDataTest : TestBase() {
    @Test
    fun initData_v2() {
        var path = System.getProperty("user.dir")

        path += "/resources/flyway/mongo/v2"

        var appMenus = mor.sys.appMenu.getMongoTemplate()
                .db
                .listCollectionNames()
                .filter { it.startsWith("appMenu-") }
                .map { it.substring("appMenu-".length) }

        var list = listOf(
                mor.admin.adminUser.query(),
                mor.admin.adminLoginUser.query()
                        .unSelect { it.errorLoginTimes },
                mor.admin.adminRole.query(),
                mor.admin.adminMenu.query(),

                mor.dev.gitServer.query(),
                mor.dev.clusterInfo.query(),
                mor.dev.gitData.query(),
                mor.dev.productLine.query(),

                mor.dev.appDeploySetting.query().where { it.code mongoEquals "dev" },
                mor.dev.userDockerfile.query(),
                mor.dev.userK8sDeployment.query()
        ) + appMenus.map { mor.sys.appMenu(it).query() }

        list.forEach { query ->
            var content = query.toList()
                    .map {
                        it.ConvertJson(JsonMap::class.java)
                    }
                    .apply {
                        this.forEach {
                            it.remove("createAt")
                            it.remove("updateAt")
                        }
                    }
                    .map { it.ToJson() }
                    .joinToString(const.line_break)
            File("${path}/${query.actualTableName}.dat").writeText(content, const.utf8)
        }
    }


    @Test
    fun initData_v0() {
        var workPath = System.getProperty("user.dir");
        var path = workPath + "/resources/flyway/mongo/v0";


        var list = listOf(
                mor.base.sysCity.query()
        )

        list.forEach { query ->
            var content = query.toList()
                    .map {
                        it.ConvertJson(JsonMap::class.java)
                    }
                    .apply {
                        this.forEach {
                            it.remove("createAt")
                            it.remove("updateAt")
                        }
                    }
                    .map { it.ToJson() }
                    .joinToString(const.line_break)
            File("${path}/${query.actualTableName}.dat").writeText(content, const.utf8)
        }
    }


    @Test
    fun test_db_gitData() {
        var c = mor.dev.productLine.query().count();
        println(c);
        println(mor.dev.productLine.query().toEntity()!!.remark);
    }
}